package com.thti.mecool.client.model.biz;

import java.util.Date;
import java.util.List; 
import java.util.Map;

import android.content.Context;

import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.TaskManage;
import com.thti.mecool.client.model.po.TaskStep;

public class TaskStepBizImpl {
	private DataBase dataBase;

	public TaskStepBizImpl(Context context) {
		dataBase = new DataBase(context);
	}

	public void save(TaskStep taskStep) {
		String sqlString = "insert into taskStep(planId,projectId,taskId,stepId,parentId,isType,actionId,name,isFinsh) values(?,?,?,?,?,?,?,?,0)";
		dataBase.execObject(
				sqlString,
				new Object[]{taskStep.getPlanId(),taskStep.getProjectId(),taskStep.getTaskId(),taskStep.getStepId(),
						taskStep.getParentId(),taskStep.getIsType(),taskStep.getActionId(),taskStep.getName()});
		dataBase.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findByjob(String planId,String projectId,String taskId,int isType){
		String sql="select * from taskStep where planId=? and projectId=? and taskId=? and isType=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,taskId,String.valueOf(isType)});
		dataBase.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findByStep(String planId,String projectId,String parentId){
		String sql="select * from taskStep where planId=? and projectId=? and isType<>0 and parentId=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,parentId});
		dataBase.close();
		return list;
	}
	
	public void updateStep(List<Map<String,Object[]>> list){
		dataBase.getWritableDatabase().beginTransaction();//开始事务
		if(list!=null && list.size()>0){
			try {
				for (int i = 0; i < list.size(); i++) {
					if(list.get(i).containsKey(HandlerConstant.PLAN_UPDATE_SQL)){
						dataBase.getWritableDatabase().execSQL(HandlerConstant.PLAN_UPDATE_SQL,list.get(i).get(HandlerConstant.PLAN_UPDATE_SQL));
					}else if(list.get(i).containsKey(HandlerConstant.UPDATE_STEP_SQL)){
						dataBase.getWritableDatabase().execSQL(HandlerConstant.UPDATE_STEP_SQL,list.get(i).get(HandlerConstant.UPDATE_STEP_SQL));
					}
				}
				dataBase.getWritableDatabase().setTransactionSuccessful();//调用此方法会在执行到endTransaction() 时提交当前事务，如果不调用此方法会回滚事务
			} finally {
				dataBase.getWritableDatabase().endTransaction();//由事务的标志决定是提交事务，还是回滚事务
			} 
			
		}
		dataBase.getWritableDatabase().close(); 
	}
	public void updateData(TaskManage taskManage){
		//update taskStep set data=?,images=?,isUpload=0,latitude=?,longitude=?,createTime=? where planId=? and projectId=?  and stepId=? and taskId=?";
	   	dataBase.getWritableDatabase().execSQL(HandlerConstant.UPDATE_STEP_SQL,new Object[]{taskManage.getData(),taskManage.getImages(),taskManage.getLatitude(),taskManage.getLongitude(),new Date(),taskManage.getPlanId(),taskManage.getProjectId(),taskManage.getStepId(),taskManage.getTaskId()});
	   	dataBase.close();
	}
	@SuppressWarnings("unchecked")
	public List<TaskStep> findFinishSteps(String projectId,String taskId,String parentId){
		String sql = "select * FROM taskStep WHERE isFinsh=1 AND projectId=? AND actionId!=9 AND taskId=? AND parentId=? AND isOver=2";
		List<TaskStep> taskSteps=dataBase.findAll(TaskStep.class, sql, new String[]{projectId,taskId,parentId});
		return taskSteps;
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findFinishSteps(String projectId,String taskId){
		String sql = "select * FROM taskStep WHERE isFinsh=1 and actionId!=9 AND projectId=? AND taskId=? AND parentId=taskId";
		List<TaskStep> taskSteps=dataBase.findAll(TaskStep.class, sql, new String[]{projectId,taskId});
		return taskSteps;
	}
	public void delete(String planId){
		String sql="delete  from taskStep where planId=?";
		dataBase.execObject(sql, new Object[]{planId});
		dataBase.close();
	}
}
